#!/bin/bash
#注意事项
#1、 本脚本是搭建mysql主从,主服务器就是执行脚本端，从服务器的ip创建slave_ip.txt从服务器要在确保ip可以ping通
# 情况下进行安装部署，如果ip有ping不通的会有提示，这时需要删除ip_up和ip_down 执行rm -rf ip_*在执行脚本
#2、最好在你想做主服务器的那台机器上执行此脚本，因为主服务器可能不唯一，所有你用那台机器做主服务器，可以
#   不用再写这台服务器的ip和密码在指定的文件中
# 3、数据目录会更改在/data/默认在/var/lib/mysql/跟着需求改，还有mysql密码，mysql主从的一些特殊配置等
user=root
slave='cat slave_ip.txt'
catalogue=/data/
login_mysql=mysql
mysql_passwd=Zg.613099
login_passwd=minerhub
for i in `$slave`
do

	ip=`echo $i|cut -d: -f1`
	passwd=`echo  $i|cut -d: -f2`
        ping -c1 $ip &>/dev/null
	if [ $? -eq 0 ];then
	echo $ip >> ~/ip_up.txt
	else
	echo $ip >>~/ip_down.txt
fi
done
if [ -f ip_down.txt ];then
	echo "有ping不通的ip，请检查当前目录下的ip_down.txt"
	exit 0
fi
#判断是否安装expect
expect -version
#[ $? -ne 0 ] && yum install -y expect && echo "expect安装成功" #centos用
[ $? -ne 0 ] && apt -y install expect && echo "expect安装成功"  #ubuntu用
#判断是否安装mysql
mysql --version
[ $? -ne 0 ] && apt update && apt -y install mysql-server && systemctl restart mysql.service && systemctl enable mysql.service && echo "mysql安装成功"
#mysql修改为utf8字符集
echo "[mysqld]" >> /etc/mysql/my.cnf && echo "" >> /etc/mysql/my.cnf && echo "character-set-server=utf8" >> /etc/mysql/my.cnf && echo "" >> /etc/mysql/my.cnf && echo "[client]" >> /etc/mysql/my.cnf && echo "" >>  /etc/mysql/my.cnf && echo "default-character-set=utf8" >> /etc/mysql/my.cnf && echo "" >> /etc/mysql/my.cnf &&  echo "[mysql]" >> /etc/mysql/my.cnf && echo "" >> /etc/mysql/my.cnf && echo "default-character-set=utf8" >> /etc/mysql/my.cnf &&  systemctl restart mysql.service 
#linux时区
apt install  ntpdate && cp /etc/localtime /etc/localtime.bak && rm -rf /etc/localtime && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && ntpdate -u ntp.api.bz
#更改mysql数据目录，mysql默认数据目录在/var/lib/mysql ,更改为/data/
systemctl stop mysql.service
[ ! -d $catalogue ] && mkdir $catalogue && cp -ar /var/lib/mysql /data/ && vim /etc/mysql/my.cnf 
sed -i 's/^datadir/#&/g' /etc/mysql/mysql.conf.d/mysqld.cnf
sed -i '36adatadir         = /data/mysql' /etc/mysql/mysql.conf.d/mysqld.cnf
sed -i 's/\(\/var\/lib\/mysql\/\ r,\)/#&/g' /etc/apparmor.d/usr.sbin.mysqld
sed -i 's/\(\/var\/lib\/mysql\/\*\*\ rwk,\)/#&/g' /etc/apparmor.d/usr.sbin.mysqld
sed -i '57a  /data/mysql/ r,' /etc/apparmor.d/usr.sbin.mysqld
sed -i '58a/data/mysql/** rwk,' /etc/apparmor.d/usr.sbin.mysqld
systemctl restart apparmor.service
if [ $? -eq 0 ];then
	echo "apparmor.service重启成功"
else	
	apt install apparmor -y && systemctl restart apparmor.service
	echo "apparmor.service安装并重启成功"
fi
systemctl restart mysql.service
echo "修改mysql目录成功"
#修改mysql密码并创建数据库
/usr/bin/expect <<-END
set timeout 2
spawn mysql
expect "mysql>" {send "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Zg.613099';\r"}
expect "mysql>" {send "flush privileges;\r"}
expect "mysql>" {send "create database hanhan;\r"}
expect "mysql>" {send "exit\r"}
expect efo
END
systemctl restart mysql.service
echo "mysql密码修改成功，密码为Zg.613099"
#mysql主配置
echo " [mysqld]" >> /etc/mysql/my.cnf
echo "log-bin=master-a-bin" >> /etc/mysql/my.cnf  #日志文件名称
echo "server-id=1" >> /etc/mysql/my.cnf  # 要求各个服务器的这个id必须一样
echo "sync_binlog = 1 " >> /etc/mysql/my.cnf  #确保binlog日志写入后与硬盘同步
echo "binlog_checksum = none" >> /etc/mysql/my.cnf  #跳过现有的采用checksum的事件，mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none
echo "binlog_format = mixed" >> /etc/mysql/my.cnf #bin-log日志文件格式，设置为MIXED可以防止主键重复。二进制日志格式 有row，statement和mixed三种类型
echo "binlog-do-db=hanhan" >> /etc/mysql/my.cnf #同步数据库的名称
echo "mysql主配置配置成功"
#mysql 从服务器配置
for i in `$slave`
do
	login_ip=`echo $i|cut -d: -f1`
	login_passwd=`echo  $i|cut -d: -f2`
/usr/bin/expect <<-END
	spawn ssh $user@$login_ip
	expect "yes/no"  { send "yes\r";exp_continue }
        expect "password:" { send  "$login_passwd\r" }
	expect "~#" { send "touch mysql_slave.sh\r" }
	expect "~#" { send "echo '#!/bin/bash' >> /root/mysql_slave.sh\r" }
	expect "~#" { send "echo mysql --version >> /root/mysql_slave.sh\r" }
	expect "~#" { send "echo '\[ \$? -ne 0 \] && apt update && apt -y install mysql-server && systemctl restart mysql.service && systemctl enable mysql.service && echo mysql安装成功' >> /root/mysql_slave.sh\r" }
	expect "~#" { send "bash mysql_slave.sh\r" }
	expect "~#" { send "echo \"\[mysqld\]\" >> /etc/mysql/my.cnf && echo \"\" >> /etc/mysql/my.cnf && echo \"character-set-server=utf8\" >> /etc/mysql/my.cnf && echo \"\" >> /etc/mysql/my.cnf && echo \"\[client\]\" >> /etc/mysql/my.cnf && echo \"\" >>  /etc/mysql/my.cnf && echo \"default-character-set=utf8\" >> /etc/mysql/my.cnf && echo \"\" >> /etc/mysql/my.cnf &&  echo \"\[mysql\]\" >> /etc/mysql/my.cnf && echo \"\" >> /etc/mysql/my.cnf && echo \"default-character-set=utf8\" >> /etc/mysql/my.cnf &&  systemctl restart mysql.service\r" }
	expect "~#" { send "apt install  ntpdate && cp /etc/localtime /etc/localtime.bak && rm -rf /etc/localtime && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && ntpdate -u ntp.api.bz \r"}
	expect "~#" { send "systemctl stop mysql.service\r" }
	expect "~#" { send "touch Mysql_slave.sh\r" }
	expect "~#" { send "echo '\[ ! -d /data \] && mkdir /data  && cp -ar /var/lib/mysql /data'/ >> Mysql_slave.sh\r" }
	expect "~#" { send "bash Mysql_slave.sh\r" }
	expect "~#" { send "sed -i 's/^datadir/#&/g' /etc/mysql/mysql.conf.d/mysqld.cnf\r" }
	expect "~#" { send "sed -i '36adatadir         = /data/mysql' /etc/mysql/mysql.conf.d/mysqld.cnf\r" }
	expect "~#" { send "touch apparmor.sh\r" }
	expect "~#" { send "echo sed -i \"'s/\\\(\\\/var\\\/lib\\\/mysql\\\/\\\ r,\\\)/#&/g'\" /etc/apparmor.d/usr.sbin.mysqld >> apparmor.sh\r" }
	expect "~#" { send "echo sed -i \"'s/\\\(\\\/var\\\/lib\\\/mysql\\\/\\\*\\\*\\\ rwk,\\\)/#&/g'\" /etc/apparmor.d/usr.sbin.mysqld >> apparmor.sh\r" }
	expect "~#" { send "bash apparmor.sh\r" }
	expect "~#" { send "sed -i '57a  /data/mysql/ r,' /etc/apparmor.d/usr.sbin.mysqld\r" }
	expect "~#" { send "sed -i '58a/data/mysql/** rwk,' /etc/apparmor.d/usr.sbin.mysqld\r" }
	expect "~#" { send "apt install apparmor && systemctl restart apparmor.service\r" }
	expect "~#" { send "systemctl restart mysql.service\r" }
        expect "~#" { send "echo \"\[mysqld\]\" >> /etc/mysql/my.cnf && echo \"log-bin=master-a-bin\" >> /etc/mysql/my.cnf && echo \"binlog_format = mixed\" >>/etc/mysql/my.cnf && echo \"server-id=2\" >>/etc/mysql/my.cnf && echo \"slave-skip-errors = all\" >> /etc/mysql/my.cnf && echo \"binlog-do-db=hanhan\" >> /etc/mysql/my.cnf \r" }
	expect "~#" { send "systemctl restart mysql.service\r" }
	
	expect efo
END

done
